package com.feng.cloud.common.configure;

import com.feng.cloud.common.configure.properties.AuthJwtProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * @ClassName TokenConfig
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/4 21:12
 * @Version 1.0
 **/
@Configuration
@EnableConfigurationProperties(AuthJwtProperties.class)
public class TokenConfigure {

    @Autowired
    private AuthJwtProperties authJwtProperties;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
    /**
     * 令牌存储策略
     * @return TokenStore
     */
    @Bean
    public TokenStore tokenStore() {
        // JWT方式
        return new JwtTokenStore(accessTokenConverter());
    }
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey(authJwtProperties.getSigningKey()); //对称秘钥，资源服务器使用该秘钥来验证
        return converter;
    }
}
